package com.example.librarysystem.Service;

import com.example.librarysystem.Dao.BooksDao;
import com.example.librarysystem.Dao.TimesDao;
import com.example.librarysystem.entity.Books;
import com.example.librarysystem.entity.Times;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("booksService")
public class BooksServiceImpl implements BooksService {
    @Autowired
    private BooksDao booksDao;
    @Autowired
    private TimesDao timesDao;

    @Override
    public Integer selectCount(){return booksDao.selectCount();}

    @Override
    public List<Books> selectAll(int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return booksDao.selectAll(startCurrentPage, pageSize);
        }
    }

    @Override
    public Integer selectScoreByName(String name){
        return booksDao.selectScoreByName(name);
    }

    @Override
    public List<Books> selectAllByLimit(int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return booksDao.selectAllByLimit(startCurrentPage,pageSize);
        }
    }

    @Override
    public List<Books> selectAllByClassification(String c, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return booksDao.selectAllByClassification(c, startCurrentPage, pageSize);
        }
    }

    @Override
    public Books selectById(int id){
        return booksDao.selectById(id);
    }

    @Override
    public List<Books> selectByISBN(String ISBN, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return booksDao.selectByISBN(ISBN, startCurrentPage, pageSize);
        }
    }

    @Override
    public List<Books> selectBySameBorrower(String bookName){
        return booksDao.selectBySameBorrower(bookName);
    }

    @Override
    public List<Books> selectBySameStar(String bookName){
        return booksDao.selectBySameStar(bookName);
    }

    @Override
    public List<Books> selectByName(String name, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return booksDao.selectByName(name, startCurrentPage, pageSize);
        }
    }
    @Override
    public List<Books> selectByPublishingHouse(String publishingHouse, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return booksDao.selectByPublishingHouse(publishingHouse, startCurrentPage, pageSize);
        }
    }
    @Override
    public List<Books> selectByPublishDate(String publishDate, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return booksDao.selectByPublishDate(publishDate, startCurrentPage, pageSize);
        }
    }
    @Override
    public List<Books> selectByAuthor(String author, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return booksDao.selectByAuthor(author, startCurrentPage, pageSize);
        }
    }
    @Override
    public List<Books> selectByISBN1(String ISBN){
        return booksDao.selectByISBN1(ISBN);
    }
    @Override
    public List<Books> selectByScoreDesc(int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return booksDao.selectByScoreDesc(startCurrentPage, pageSize);
        }
    }
    @Override
    public List<Books> selectBookByClickDesc(int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return booksDao.selectBookByClickDesc(startCurrentPage, pageSize);
        }
    }
    @Override
    public List<Books> selectBookBySearchDesc(int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return booksDao.selectBookBySearchDesc(startCurrentPage, pageSize);
        }
    }
    @Override
    public List<Books> selectBookByBorrowDesc(int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return booksDao.selectBookByBorrowDesc(startCurrentPage, pageSize);
        }
    }
    @Override
    public List<Books> selectBookByStarDesc(int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return booksDao.selectBookByStarDesc(startCurrentPage, pageSize);
        }
    }
    @Override
    @Transactional(timeout = 30)
    public int updateScoreByName(int score, String name){
        booksDao.updateScoreByName(score, name);
        return 0;
    }
    @Override
    public Books selectByNameOnly(String name){
        return booksDao.selectByNameOnly(name);
    }
    @Override
    @Transactional(timeout = 30)
    public int updateBookBorrow(int id, boolean borrown){
        booksDao.updateBookBorrow(id,borrown);
        return 0;
    }
    @Override
    @Transactional(timeout = 30)
    public int insertBook(Books books, Times times){
        if(times != null){
            timesDao.insertTimes(times);
        }
        if(booksDao.insertBook(books) == 1){
            return books.getId();
        }
        return 0;
    }
    @Override
    @Transactional(timeout = 30)
    public int updateById(Books books){
        booksDao.updateById(books);
        return 0;
    }
    @Override
    @Transactional(timeout = 30)
    public int deleteById(int id){
        String name = booksDao.selectById(id).getName();
        booksDao.deleteById(id);
        Books books = booksDao.selectByNameOnly(name);
        System.out.println("删书籍");
        System.out.println(books);
        if(books == null){
            timesDao.deleteByName(name);
        }
        return 0;
    }
}
